<?php 
ini_set('display_errors', 1); 
ini_set('error_reporting', 'E_error'); 
 require_once "../includes/config.php";
 ?>
<script type="text/javascript">
function funViewMsg(fid) {
	window.location='index.php?viewmessages&fid='+fid;
}
function funDelMsg(fid) {
var delPro=confirm("Are you sure want to delete message?");
if(delPro==true) {
       var frm = document.getElementById('form1');
       document.getElementById("hidefid").value=fid;
       frm.submit();
}	
}
</script>
<?
require_once "../includes/pagination.php";
if(!empty($_POST['hidefid']))
{
       $fid=$_POST['hidefid'];
       $sqlDelMem="Delete from tbl_messages where messageid=$fid";
       $rstDelMem=mysql_query($sqlDelMem);	
}

$cond = '';
$sort = '';
$ut = $u = $fn = $ln = $dg = 'asc';
if(isset($_GET['sort']))
{
 if($_GET['sort']=='posteddate')
 {
  if($_GET['s_type']=='asc')
  {	$cond=' ORDER BY posteddate ASC';	$sort = '&sort=posteddate&s_type=asc';	$ut='desc';	}
  else
  {	$cond=' ORDER BY posteddate DESC';	$sort = '&sort=posteddate&s_type=desc';	}
 }
 
}
 
?>
<form action="" method="post" name="form1" id="form1" name="formService" id="formService">
<input type="hidden" name="hidefid" id="hidefid">
<table width="100%"  border="1" cellspacing="0" class="container">
       <tr>
	 <td width="469" height="25" class="tdHeader">Member Messages <span style="text-align:right; padding-left:350px;"></span></td>
       </tr>  
       <tr>
	      <td height="577">	
		     <table width="100%">
			    <tbody>
				   <tr>
					  <td>
						 <form name="frmSearchPro" action="" method="post">
							<table width="100%" border="0">
							       <tr>
								      <td width="20%">Sender</td>
								      <td width="30%">
									      <input type="text" name="sender" id="sender" style="width: 150px;" value="<?php echo $_POST['sender']?>" />
								      </td>
								      <td width="20%">Receiver</td>
								      <td width="30%"><input type="text" name="receiver" id="receiver" style="width: 150px;" value="<?php echo $_POST['receiver']?>" /></td>
							       </tr>
							      
				  
							       <tr>
								       <td height="35" colspan="5" style="text-align:center"><input type="submit" name="btnSearchPro" value="Search" /></td>
							       </tr>
							</table>
						 </form>
					  </td>
				   </tr>
				   <tr>
					  <td>
						 <b>Sort  By :</b>
						 <a href="<?php echo $base_url;?>admin/index.php?mem_messages&sort=posteddate&s_type=<?php echo $ut;?>">Post date</a> &nbsp;&nbsp;&nbsp;
					  </td>
				   </tr>
			    </tbody>
		     </table><br>
		     <table width="100%"  border="0" cellspacing="0">
			    
			    <tr class="InnerHeader">
			      <!--<td width="40%" class="line">Subject</td>-->
			      <td width="41%" class="line">Message</td>
			      <td width="41%" class="line">Sender</td>
			      <td width="41%" class="line">Receiver</td>
                  <td width="41%" class="line">Post Date</td>
			      <td width="9%" class="line"></td>
			       <td width="9%" class="line"></td>
			    </tr>	 
			    <?php
			    $itemPerPage=15;
			    $search='';
			    if(isset($_REQUEST['btnSearchPro'])) {
				    
				    $sender 	    = $_REQUEST['sender'];		
				    $receiver 		= $_REQUEST['receiver'];
				   
				   $pglink          = '&btnSearchPro=1&sender='.$_REQUEST['sender'].'&receiver='.$_REQUEST['receiver'];
				   
				    
				   if($sender!='')
				   $search.= "where senderid IN (select user_id from tbl_users where username LIKE '%$sender%')";
				   
				 				   
				   
//				   if($receiver!='')
//				   $search.= "where recieverid IN (select user_id from tbl_users where username LIKE '%$receiver%')";
				   
				   	if($sender!='') {
					  if($receiver!='')
						 $search.=" AND recieverid IN (select user_id from tbl_users where username LIKE '%$receiver%')";
				   }
				   else {
					  if($receiver!='')
						 $search.="where recieverid IN (select user_id from tbl_users where username LIKE '%$receiver%')";
				   }

				    
			    }
			    $sqlShwMsg="select * from tbl_messages $search $cond";
				//$sqlShwMsg="select * from tbl_messages";
				//echo $sqlShwMsg;
			    $rstShwMsg=mysql_query(getPagingQuery($sqlShwMsg,$itemPerPage));
			    $numShwMsg=mysql_num_rows($rstShwMsg);
			    if($numShwMsg==0) 
			    {
				   echo "<tr><td></td><td colspan='4'><h3>No messages Found...</h3></td></tr>";
			    }
			    else 
			    {
				   while($rowShwMsg=mysql_fetch_array($rstShwMsg)) 
				   {
					  $msgId=$rowShwMsg['messageid'];
					  $subject=stripslashes($rowShwMsg['subject']);
					  $message=substr(stripslashes($rowShwMsg['message']),0,15)."...";
					  $post_date=$rowShwMsg['posteddate'];
					  $selUser=mysql_query("select * from tbl_users where user_id='".$rowShwMsg['senderid']."'");
					  $rowUser=mysql_fetch_array($selUser);
					  $selUser2=mysql_query("select * from tbl_users where user_id='".$rowShwMsg['recieverid']."'");
					  $rowUser2=mysql_fetch_array($selUser2);
					  $sender=$rowUser['username'];
					  $receiver=$rowUser2['username'];
					  echo "<tr>";
					  //echo "<td class='line'>$subject</td>";
					  echo "<td class='line'>$message</td>";
					  echo "<td class='line'>$sender</td>";
					  echo "<td class='line'>$receiver</td>";
                      echo "<td class='line'>$post_date</td>";
					   echo "<td class='line'><input type='button' name='view' value='View' class='button'  onclick='funViewMsg($msgId)'></td>";
					  echo "<td height='20' class='line'><input type='button' name='delete' value='Delete' class='button'  onclick='funDelMsg($msgId)';></td> ";
					  echo "<td height='20' class='line'></td> ";
					  echo "</tr>";
				   }
			    }
			    ?>
			    <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
		     </table>
<div align="center">
		<?php		   
		   $link="mem_messages".$pglink;
		   echo $disp=getPagingLink($sqlShwMsg,$itemPerPage,$link,$sort);							   
		?>    
	</div></td>
       </tr>
</table>
</form>